package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Card;


@Component
public class CardDAO {
	 

	 @PersistenceContext
	 private EntityManager em;
	/**
	 * @return all cards from Card table
	 */
	 @Transactional
	public List<Card> getCards() {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Card> query = em.createQuery("SELECT a FROM Card a", Card.class);
		List<Card> cards = null;
		cards = query.getResultList();
		return cards;
	}
	
	/**
	 * Add card to Card table
	 * @param card Card entity that contains mapped with each column in Card table
	 * @return
	 */
	@Transactional
	public boolean addCard(Card card) {
//		EntityManager em = TransactionManager.getEntityManager();
		em.persist(card);
		return true;
	}
	/**
	 * 
	 * @param loginId
	 * @return all card of the user logged in
	 */
	@Transactional
	public List<Card> getCardByCustomer(String loginId) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Card> query = em.createQuery("SELECT c FROM Card c WHERE c.customer.loginId = :loginId", Card.class);
		query.setParameter("loginId", loginId);
		List<Card> accounts = null;
		accounts = query.getResultList();
		return accounts;
	}
}
